from rest_framework import status
from rest_framework.generics import RetrieveAPIView
from rest_framework.permissions import IsAuthenticated

from Group.models import TeamMemberShip
from Group.permission import IsTeamMemberSelfOrAdmin
from Group.serializers import TeamIndexMemberInfoSerializers
from Person.authenticate import MyJSONWebTokenAuthentication
from Utils.BaseResponse import BaseResponse


class TeamIndexMemberInfoView(RetrieveAPIView):
    authentication_classes = [MyJSONWebTokenAuthentication]  # 登录认证
    permission_classes = [IsAuthenticated, IsTeamMemberSelfOrAdmin]

    def get(self, request, *args, **kwargs):
        pk = self.kwargs.get('pk')
        member = TeamMemberShip.objects.filter(team_id=pk, person_id=request.user.id)
        if not member.exists():
            return BaseResponse(status=status.HTTP_404_NOT_FOUND).fail("请求参数错误")
        serializer = TeamIndexMemberInfoSerializers(member.first())
        return BaseResponse(data=serializer.data, status=status.HTTP_200_OK).success()
